﻿Imports ControleMatricula.Negocio

Public Class FrmAlunoConsultar
    Dim db As New DataClassesDataContext()
    Friend RowAluno As SPAlunoConsultarResult
    Friend RowResponsavel As SPResponsavelConsultarResult
    Friend RowPagamento As SPAlunoPagamentoConsultarResult
    Friend RowEspecifica As SPAlunoEspecificaConsultarResult

    Private Sub btnConsultar_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnConsultar.Click

        Dim retorno As List(Of SPAlunoConsultarResult)
        Dim retornoEspecifica As List(Of SPEspecificaConsultarResult)

        retornoEspecifica = db.SPEspecificaConsultar(0, Convert.ToString(cmbEspecifica.Text)).ToList()

        Dim Matricula As Integer
        Dim Nome As String
        Dim CPFResponsavel As String
        Dim NomeResponsavel As String
        Dim Especifica As Integer

        If txtConsultaAluno.Text <> "" Then
            If IsNumeric(txtConsultaAluno.Text) Then
                Matricula = Convert.ToInt32(txtConsultaAluno.Text)
                Nome = ""
            Else
                Matricula = 0
                Nome = Convert.ToString(txtConsultaAluno.Text)
            End If
        Else
            Matricula = 0
            Nome = ""
        End If

        If txtConsultaResp.Text <> "" Then
            If IsNumeric(txtConsultaResp.Text) Then
                CPFResponsavel = Convert.ToInt32(txtConsultaResp.Text)
                NomeResponsavel = ""
            Else
                CPFResponsavel = ""
                NomeResponsavel = Convert.ToString(txtConsultaResp.Text)
            End If
        Else
            CPFResponsavel = ""
            NomeResponsavel = ""
        End If

        If cmbEspecifica.Text <> "" Then
            Especifica = retornoEspecifica(0).IDEspecifica
        Else
            Especifica = 0
        End If

        Dim Ativo As Char
        If STMatricula.Checked Then
            Ativo = "1"
        Else
            Ativo = "0"
        End If

        retorno = db.SPAlunoConsultar(0, _
                                      Matricula, _
                                      Nome, _
                                      "", _
                                      CPFResponsavel, _
                                      NomeResponsavel, _
                                      Especifica, _
                                      Ativo, 0, 0).ToList()

        listAluno.DataSource = retorno
        listAluno.DisplayMember = "Nome"
        listAluno.ValueMember = "IDAluno"

        listAluno.Focus()

    End Sub

    Sub Selecionar()

        Try
            Dim RetornoAluno As List(Of SPAlunoConsultarResult)
            RetornoAluno = db.SPAlunoConsultar(listAluno.SelectedValue, 0, "", "", "", "", 0, "", 0, 0).ToList()

            RowAluno = RetornoAluno(0)

            Dim RetornoResponsavel As List(Of SPResponsavelConsultarResult)
            RetornoResponsavel = db.SPResponsavelConsultar(listAluno.SelectedValue, 0).ToList()

            RowResponsavel = RetornoResponsavel(0)



            Me.Close()

        Catch Er As Exception
            MsgBox(Er.Message, MsgBoxStyle.Critical, "Específica")
        Finally
            Me.Cursor = System.Windows.Forms.Cursors.Default
        End Try

    End Sub

    Private Sub listAluno_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles listAluno.KeyDown

        If listAluno.Items.Count > 0 Then
            If e.KeyData = Keys.Enter Then
                Call Selecionar()
            End If
        End If

    End Sub

    Private Sub listAluno_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles listAluno.MouseUp

        If listAluno.Items.Count > 0 Then
            Call Selecionar()
        End If

    End Sub

    Private Sub FrmAlunoConsultar_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        MontarComboEspecifica(cmbEspecifica)
        cmbEspecifica.SelectedIndex = -1

    End Sub
End Class